import pandas
from django.forms import model_to_dict
from django.http import JsonResponse, HttpResponse
import requests
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from pandas._libs import json
from django.contrib.auth.hashers import make_password, check_password
from .models import dev, user

@require_http_methods(['GET'])
def add_dev(request):
    print("add-start")
    devId = request.GET.get('devId')
    channelNo = request.GET.get('channelNo')
    channel = request.GET.get('channel')
    terminal_id = request.GET.get('terminal_id')
#     print(terminal_id)
#     print("--")
#     print(devId)
#     print(channel)
#     dev.objects.create(terminal = terminal_id,devId = devId,channelNo = channelNo,channel = channel)
    if dev.objects.create(terminal = terminal_id,devId = devId,channelNo = channelNo,channel = channel):
        response = {"res": "ok"}
    else:
        response = {"res": "no"}
    response = {"res": "ok"}
    return JsonResponse(response)


# @require_http_methods(['GET'])
# def add_rtsp(request):
#     id1 = request.GET.get('id')
#     res_name = request.GET.get('res_name')
#     camid = request.GET.get('camid')
#     rtsp1 = request.GET.get('rtsp')
#     cam_name = request.GET.get('cam_name')
#     terminal_id = request.GET.get('terminal_id')
#     rtsp.objects.create(res_id=id1, res_name=res_name, camid=camid, rtsp=rtsp1, cam_name=cam_name,
#                         terminal_id=terminal_id)
#     response = {"res": "ok"}
#     return JsonResponse(response)


# @require_http_methods(['GET'])
# def see_rtsp(request):
#     response = []
#     res_id = request.GET.get('res_id')
#     terminal_id = request.GET.get('terminal_id')
#     rtsps = rtsp.objects.filter(res_id=res_id, terminal_id=terminal_id)
#     for s_rtsp in rtsps:
#         response.append({'rtsp': s_rtsp.rtsp})
#     return JsonResponse(response, safe=False)


# @require_http_methods(['GET'])
# def add_idconfig(request):
#     res_id = request.GET.get('res_id')
#     config = request.GET.get('config')
#     capturedserver = request.GET.get('capturedserver')
#     interval = request.GET.get('interval')
#     terminal_id = request.GET.get('terminal_id')
#     idconfig.objects.create(res_id=res_id, config=config, capturedserver=capturedserver, interval=interval, terminal_id=terminal_id)
#     response = {"res": "ok"}
#     return JsonResponse(response, safe=False)


@require_http_methods(['GET'])
def login(request):
    username = request.GET.get('name')
    password = request.GET.get('psw')
    res = user.objects.filter(user_name=username)
#     result = check_password(password, res.first().user_password)
    if res.first().user_password == password :
        response = {"info": "ok"}
    else:
        response = {"info": "no"}
    return JsonResponse(response, safe=False)


@require_http_methods(['GET'])
def search_terminal(request):
    response = []
    results = dev.objects.all()
    for result in results:
        demo = []
#         print(result.terminal)
        demo.append(result.terminal)
        demo.append(result.devId)
        response.append(demo)

    return HttpResponse(json.dumps(response), content_type="application/json")


@require_http_methods(['GET'])
def search_idconfig_by_terminal(request):
    response = []
    terminal = request.GET.get('terminal')
    id_config = dev.objects.filter(terminal=terminal)
    if id_config:
        devId = id_config.first().devId
        channelNo = id_config.first().channelNo
        channel = id_config.first().channel
        response.append({'terminal': terminal,'devId': devId, 'channelNo': channelNo, 'channel': channel})
    else:
        response.append({'res': 'no'})

    return JsonResponse(response, safe=False)


# @require_http_methods(['GET'])
# def del_rtsp_idconfig_by_terminal(request):
#     terminal_id = request.GET.get('terminal_id')
#     rtsp.objects.filter(terminal_id=terminal_id).delete()
#     idconfig.objects.filter(terminal_id=terminal_id).delete()
#     response = {'res': 'yes'}
#     return JsonResponse(response, safe=False)

@require_http_methods(['GET'])
def del_idconfig_by_terminal(request):
    terminal_id = request.GET.get('terminal_id')
    dev.objects.filter(terminal=terminal_id).delete()[0]
    response = {"res": "ok"}
    return JsonResponse(response)

# 传递数据库中的参数到前端
@require_http_methods(['GET'])
def see_config(request):
    response = []
    terminalall = dev.objects.all()
    index = 0
    for t in terminalall:
        response.append({"terminal":t.terminal,"devId":t.devId,"channelNo":t.channelNo,"channel":t.channel})
    print(json.dumps(response))
    return HttpResponse(json.dumps(response), content_type="application/json")

#     delete camera from 前端
@require_http_methods(['GET'])
def del_idconfig_by_terminal(request):
    terminal_id = request.GET.get('terminal_id')
    dev.objects.filter(terminal=terminal_id).delete()
    response = {'res': 'yes'}
    return JsonResponse(response, safe=False)